package com.cogent.business.helper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.cogent.business.cache.UserCacheManager;
import com.cogent.business.domain.UserDomain;
import com.cogent.business.domain.WebUserDomain;
import com.cogent.common.util.Globals;
import com.cogent.common.util.MapUtil;
import com.cogent.common.util.StringUtil;
import com.cogent.dao.BussinessInstance;
import com.cogent.dao.domain.QueryParamDomain;
import com.cogent.dao.facade.BussinessDaoFacade;
import com.cogent.dao.type.QueryType;

public class UserDomainHelper {
	//获取用户信息
	public static UserDomain getUser(HttpServletRequest request) {
		UserDomain userDomain = null;
		Object userid = request.getSession().getAttribute("userid");
		Object obj = null;
		if(userid != null)
			obj = UserCacheManager.get(userid.toString());
		if(obj != null)
			userDomain = (UserDomain)obj;
		if(userDomain == null) 
			userDomain = webUserToSysUser(request);
		
		return userDomain;
	}
	
	private static UserDomain webUserToSysUser(HttpServletRequest request) {
		UserDomain userDomain = null;
		String webid = "";
		Object obj = request.getSession().getAttribute("webuserid");
		if(obj != null)
			webid = obj.toString();
		if(StringUtil.isEmpty(webid))
			return userDomain;
		WebUserDomain webuserdomain = getWebUser(request);
		if((webuserdomain != null) && (!StringUtil.isEmpty(webuserdomain.getAdminid()))) {
			BussinessInstance bussinessinstance = new BussinessInstance();
			BussinessDaoFacade bussinessFacade = bussinessinstance.getBussinessFacade();
			List queryList = new ArrayList();
			queryList.add(new QueryParamDomain("id", webuserdomain.getAdminid()));
			queryList.add(new QueryParamDomain("status", "0"));
			try{
				bussinessFacade.createConn();
				HashMap userMap = bussinessFacade.getByID("admin", queryList);
				if(userMap.size() > 0) {					
					UserDomain userdomain = new UserDomain();
					userdomain.setId(MapUtil.getStringValue(userMap, "id"));
					userdomain.setUsername(MapUtil.getStringValue(userMap, "username"));
					 
					 ;
					UserCacheManager.put(userdomain.getId(), userdomain);
					request.getSession().setMaxInactiveInterval(StringUtil.toInt(Globals.getProperty("sessiontime"), 300000000));
					request.getSession().setAttribute("userid", userdomain.getId());
				}
			}finally {
				bussinessFacade.closeConn();
			}
		}
		
		return userDomain;
	}
	
	
	public static WebUserDomain getWebUser(HttpServletRequest request) {
		WebUserDomain userDomain = null;
		Object userid = request.getSession().getAttribute("webuserid");
		Object obj = null;
		if(userid != null)
			obj = UserCacheManager.get(userid.toString());
		if(obj != null)
			userDomain = (WebUserDomain)obj;
		
		return userDomain;
	}
	
	//权限校验
	public static boolean isAllow(HttpServletRequest request, String resCode, int operation) {
		boolean result = true;
		UserDomain userdomain = getUser(request);
		if(userdomain == null)
			return false;
//		String[] temps = resCode.split(";");
//		for(int i=0; i<temps.length; i++) {
//			int curvalue = MapUtil.getIntValue(userdomain.getUserPermissionMap(), temps[i], 0);
//			if((curvalue & operation) == operation) {
//				result = true;
//				break;
//			}
//		}
		
		return result;
	}
	
	 
}